<?

/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date 
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$thisSequence   = "PCH_SEQ";

$srcDbSequence      = "MST_SEQUENCE";
$srcDbMstCategory   = "MST_MODEL_CAT";
$srcDbMstGroup      = "MST_MODEL_GRP";
$srcDbHdr			      = "PCH_RECV_HDR";
$srcDbHdrLog	      = "LOG_PCH_RECV_HDR";
$srcDbDtl           = "PCH_RECV_DTL";
$srcDbConvertion    = "MST_MODEL_UNIT";

$UID     = unserialize($_SESSION["serUID"]);
$userId  = $UID->getUID();

$CHandlers = new CGHandlers;
$CFetchers = new CGFetchers;

$COracle = new oracleDb;
$COracle->init($_SESSION["ORCL"], 
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

if($COracle->connect()){
	
	/** 
	* Delete the previous entry
	*
	*/
	$CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbHdr);
	            
  $CHandlers->dbEntDelete($COracle,
            	            $_SESSION["jid"],
            	            $srcDbDtl);
	            
  /**
  * We don't build up any jobId anymore since this is
  * a modify mode. Also we already have the previous
  * jobId in $_SESSION
  *
  */
  $jobId = $_SESSION["jid"];
  
	/**
  * Insert all data
  *
  */
  for($iRow = 0; $iRow < $dataup; $iRow++){
    
    fetchIdCategory($COracle,
                    $_POST["catEnt"][$iRow],
                    $srcDbMstCategory,
                    $idCategory);
    
    fetchIdGroup($COracle,
                 $_POST["grpEnt"][$iRow],
                 $srcDbMstGroup,
                 $idGroup);
    
    calcRecvQty($COracle,
                $_POST["lstCvtEnt"][$iRow],
                $_POST["cvtEnt"][$iRow],
                $_POST["unitEnt"][$iRow],
                $_POST["recvQtyEnt"][$iRow],
                $srcDbConvertion,
                $recvQty);
    
    if($recvQty > $_POST["qtyEnt"][$iRow]){
      
      usrDie("Error: Received Quantity larger than order quantity");
    }
    
    $odrDate = " TO_DATE('".$_POST["odrDateEnt"][$iRow]."', 'dd-mm-YYYY') ";
    
    $values = " '".$jobId."', ".                      /* Job id     */
              "  ".$odrDate." , ".                    /* Order date */
              " '".$_POST["odrRefEnt"][$iRow]."', ".  /* Order ref  */
              "  ".$_POST["odrNoEnt"][$iRow]." , ".   /* Order No   */
              "  ".$idCategory." , ".                 /* Cat id     */
              "  ".$idGroup." , ".                    /* grp id     */
              " '".$_POST["modelEnt"][$iRow]."', ".   /* Model name */
              "  ".$_POST["modelIdEnt"][$iRow]." , ". /* Model Id   */
              " '".$_POST["unitEnt"][$iRow]."', ".    /* Model unit */
              "  ".$_POST["qtyEnt"][$iRow]." , ".     /* Model qty  */
              " '".$_POST["cvtEnt"][$iRow]."', ".     /* Model converted to */
              "  ".$_POST["cvtQtyEnt"][$iRow]." , ".  /* Model converted qty */
              "  ".$recvQty." , ".                    /* Receive Qty */
              "  ".$_POST["priceEnt"][$iRow]." , ".   /* Model price */
              "  ".$_POST["hrzTotEnt"][$iRow].",  ".  /* horizontal total */
              "  ".($iRow + 1)."  ";                  /* no, incr-ed by 1 since $iRow starts from 0 */
    
    insToDbDtl($COracle,
               $values,
               $srcDbDtl);
  }
  
  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."', 'dd-mm-YYYY')";
                     
  $CFetchers->fetchHdrNo($COracle,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);
  
  $values = " '".$jobId."', ".                /* Job id */
            " ' ',    ".                      /* Reference id ' ' default */
            "  ".$hdrDate." , ".              /* Tx date */
            "  ".$hdrNo.", ".                 /* Number */
            " '"._NOT_ACTIVE."' ";            /* Status */
  
  
  insToDbHdr($COracle,
             $values,
             $srcDbHdr);           
            
  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($COracle,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);
                                
	$COracle->commit();
	
	if($COracle->getStatus() == true){
	
	  usrMsg("Modify Complete");
	}else{
	  
	  usrMsg("Modify Process Interupted");
	}
	
	$COracle->dc();
	
	$restore = 0;
	$uprun = 0;
	$dataup = 0;
	
	//jumpTo(2,"../".$_SESSION["source_caller"]);
	
}else{
	
	echo "<br/><font size='16'>Error reason: upload connection failed </font>\n";
}

?>